package com.hosever.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * 补丁生成器
 * 
 * @author Sever Ho
 * 
 */
public class PatchMaker {
	public static void main(String[] args) {
		// 生成补丁
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String todayString = sdf.format(new Date());
		System.out.println("请输入【系统（修改内容）】：");
		Scanner sc = new Scanner(System.in);
		String title = sc.nextLine();
		String patchName = todayString+title;
		
		/*
		 * D:\\jboss-4.2.2.GA\\server\\default\\deploy\\
		 * D:\\heshihua\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp1\\wtpwebapps\\
		 */
		
		String oldRoot = "D:\\jboss-4.2.2.GA\\server\\default\\deploy\\";
		String newRoot = "D:\\heshihua\\补丁包\\" + patchName + "\\";
		// String newRoot =
		// "D:\\heshihua\\补丁包\\20150521清远网办补丁包（镇街服务事项列表承诺期限）\\";
		// String dir =
		// "publicServices.war\\portal\\street\\serviceSubject\\webpart\\";
		// String fileName = "searchResult.xhtml";

		System.out.println("请输入文件清单：");
		Scanner scanner = new Scanner(System.in);
		List<String> dirFileList = new ArrayList<String>();
		while (true) {
			String string = scanner.nextLine();
			if (string.equals("")) {
				break;
			}
			dirFileList.add(string);
		}
		for (String dirFile : dirFileList) {
			String[] dirFileArray = dirFile.split("\\\\");
			String fileName = dirFileArray[dirFileArray.length - 1];
			String dir = dirFile.replace(fileName, "");
			try {
				FileUtils.copyFile(oldRoot + dir, newRoot + dir, fileName);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		//生成说明文件
		try {
			System.out.println("===生成说明文件===");
			File directory = new File(newRoot);
			String prefix = "【说明】"+patchName;
			String suffix = ".txt";
//			File file = File.createTempFile(prefix, suffix, directory);
			
			File file =new File(directory, prefix+suffix);
			if(!file.exists()){
				file.createNewFile();
			}
			
			String data = "文件清单：\n";
			for (String dirFile : dirFileList) {
				data+=dirFile+"\n";
			}
			data+="\n";
			data+="说明：\n";
			data+="覆盖文件";
			
			FileWriter fileWritter = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
            bufferWritter.write(data);
            bufferWritter.close();
			
			System.out.println(newRoot+prefix+suffix);
			
		} catch (IOException e) {
			e.printStackTrace();
		} 
		
	}
}
